package universe;

import java.util.ArrayList;

import junit.framework.TestCase;

public class BoardTest extends TestCase {
	private Board B;
	private int width=0;
	private int height=0;
	private ArrayList<int[][]> blockA;


	/*
	private static ArrayList<int[][]> getBlockA(){

		ArrayList<int[][]> blockA = new ArrayList<int[][]>();
		int[][] pos0 = {{0,1,0,0},{0,1,1,0},{0,0,1,0},{0,0,0,0}};
		int[][] pos1 = {{0,0,0,0},{0,0,1,1},{0,1,1,0},{0,0,0,0}};
		int[][] pos2 = {{0,1,0,0},{0,1,1,0},{0,0,1,0},{0,0,0,0}};
		int[][] pos3 = {{0,0,0,0},{0,0,1,1},{0,1,1,0},{0,0,0,0}};
		blockA.add(pos0);
		blockA.add(pos1);
		blockA.add(pos2);
		blockA.add(pos3);
		return blockA;
	}*/
	
	private static ArrayList<int[][]> getBlockA(){
		return Utils.getBlockA();
	}

	protected void setUp() throws Exception {
		super.setUp();
		width=12;
		height=20;
		blockA = BoardTest.getBlockA();
		B = new Board(width, height);
	}

	protected void tearDown() throws Exception {
		super.tearDown();
	}

	public void testBoard() {

		assertTrue(B.getWidth() == width);
		assertTrue(B.getHeight() == height);

		int[][] matrix = B.getMatrix();

		assertTrue(matrix[0][0] == 1);
		assertTrue(matrix[width-1][height-1] == 1);
		for(int x = 1; x < width-1; ++x){
			for(int y=1; y < height-1; ++y){
				assertTrue(matrix[x][y] == 0);
			}
		}

	}

	public void testPushBlock() {
		int blockWidth = Utils.BLOCK_WIDTH;
		int blockHeight = Utils.BLOCK_HEIGHT;
		Block b2 = new Block(blockA);
		int xpos=3;
		int ypos=3;
		
		B.pushBlock(b2, xpos, ypos);
		for ( int line=0 ; line < blockHeight; line++){ 
			for ( int col=0 ; col < blockWidth; col++){
				assertTrue(b2.getMatrix()[line][col]== B.getMatrix()[xpos+col][ypos+line]);
			}
		}
		
		int xpos2=5;
		int ypos2=3;
		B.pushBlock(b2, xpos2, ypos2);
		assertTrue(B.getMatrix()[xpos2][ypos2+1]!=0);
		assertTrue(B.getMatrix()[xpos2][ypos2+2]!=0);
	}

	public void testPushAllowed() {
		Block bl = new Block(blockA);

		assertTrue(B.pushAllowed(bl, 3, 3));
		assertFalse(B.pushAllowed(bl, width, height));
		assertFalse(B.pushAllowed(bl, width-3, height-4));
		assertFalse(B.pushAllowed(bl, width-4, height-3));


		B.getMatrix()[2][height-2] = 1;
		assertTrue(B.pushAllowed(bl, 0, height-5));
		assertFalse(B.pushAllowed(bl, 0, height-4));

	}

	public void testDetectLowestCompletedLine(){
		int[][] matrix = B.getMatrix();
		
		int line1 = 4;
		
		for(int i=1; i < width-1; ++i){
			matrix[i][line1] = 1;
		}
		
		int line2 = 9;
		
		for(int i=1; i < width-1; ++i){
			matrix[i][line2] = 1;
		}
		
		int detected = B.detectLowestCompletedLine(height-2);
		assertTrue("Function did not detect line "+line2+"returned value "+detected,
				detected==line2);
		
		detected = B.detectLowestCompletedLine(line2);
		assertTrue("Function did not detect line "+line2+" returned value "+detected,
				detected==line2);
		
		detected = B.detectLowestCompletedLine(line2-1);
		assertTrue("Function did not detect line "+line1+" returned value "+detected,
				detected==line1);
	}
	
	public void testCollapseLine(){
		int[][] matrix = B.getMatrix();
		
		int max=width-1;
		
		int[] tab1 = new int[width];
		for(int i=0; i<4; ++i){
			tab1[i]=1;
		}
		tab1[max]=1;
		
		int[] tab2 = new int[width];
		tab2[0]=1;
		for(int i=3; i<6; ++i){
			tab2[i]=1;
		}
		tab2[max]=1;
		
		
		int line1 = 4;
		int line2 = 5;
		
		for(int i=0; i<width; ++i){
			matrix[i][line1]=tab1[i];
		}
		for(int i=0; i<width; ++i){
			matrix[i][line2]=tab2[i];
		}
		
		B.collapseLine(line2);
		for(int i=0; i<width; ++i){
			assertTrue(matrix[i][line2]==tab1[i]);
		}
	}
}
